package com.GlobalPaint.app.ui.Mine;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.MyShouCAdapter;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.LinshiWirkBean;
import com.GlobalPaint.app.bean.PaletteDetailsBean;
import com.GlobalPaint.app.bean.RecruitBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyShouCActivity extends AppCompatActivity {
    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)

    TextView tvCommonCenter;
    private MyShouCAdapter adapter;
    private int page = 1;
    private Dialog dlgWaiting2;
    private LinshiWirkBean bean;
    private XRecyclerView shouchang_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private List<PaletteDetailsBean.DataBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_shou_c);
        ButterKnife.bind(this);
        tvCommonCenter.setText("我的收藏");
        initView();
    }

    private void initView() {
        shouchang_xrecyler = (XRecyclerView) findViewById(R.id.shouchang_xrecyler);
        linearLayoutManager = new LinearLayoutManager(this);
        shouchang_xrecyler.setLayoutManager(linearLayoutManager);
        shouchang_xrecyler.setLoadingMoreEnabled(false);
        getList();
        initLoad();
    }

    private void getList() {
        String url = Constants.JASON_SERVICE_URL + "cmsfavorite/applist?userId=" + DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PaletteDetailsBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                PaletteDetailsBean paletteBean = (PaletteDetailsBean) o;
                if (paletteBean.getStatus() == 1) {
                    if (paletteBean.getData() != null && paletteBean.getData().size() > 0) {
                        list = paletteBean.getData();
                        adapter = new MyShouCAdapter(list, MyShouCActivity.this);
                        shouchang_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else {
                        list=new ArrayList<PaletteDetailsBean.DataBean>();
                        adapter = new MyShouCAdapter(list, MyShouCActivity.this);
                        shouchang_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }

                shouchang_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if (list != null && list.size() > 0) {
                    list.clear();
                    Toast.makeText(MyShouCActivity.this, "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    shouchang_xrecyler.refreshComplete();

                }
            }
        });
    }

    public void deleteUser(final int id ,final int position) {
        String url = Constants.JASON_SERVICE_URL + "cmsfavorite/delete?did=" + id+
                "&userId="+DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean = (LinshiWirkBean) o;
                if (bean.getStatus() == 1) {
                    list.remove(position);
                    adapter.notifyDataSetChanged();
                    Log.d("MyshouCActivity", "deleteUser: ");
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {


            }
        });
    }
    private void initLoad() {
        shouchang_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url = Constants.JASON_SERVICE_URL + "cmsfavorite/applist?page=" + 1 +
                        "&userid=" + DataManager.userEntity.getUserId();
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PaletteDetailsBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        PaletteDetailsBean paletteBean = (PaletteDetailsBean) o;
                        if (paletteBean.getStatus() == 1) {
                            if (paletteBean.getData() != null && paletteBean.getData().size() > 0) {
                                if (paletteBean.getData() != null && paletteBean.getData().size() > 0) {
                                    list = paletteBean.getData();
                                    adapter = new MyShouCAdapter(list, MyShouCActivity.this);
                                    shouchang_xrecyler.setAdapter(adapter);
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        }
                        shouchang_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        shouchang_xrecyler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
                page++;
                String url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/list?page=" + page + "&userid=" +
                        DataManager.userEntity.getUserId();
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), RecruitBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        RecruitBean jobBean = (RecruitBean) o;
                        if (jobBean.getStatus() == 1) {
                            if (jobBean.getData() != null && jobBean.getData().size() > 0) {
                                //  list.addAll(jobBean.getData());
                            }
                        } else {
                            page = page - 1;
                            Toast.makeText(MyShouCActivity.this, "暂无更多数据", Toast.LENGTH_LONG).show();
                        }
                        shouchang_xrecyler.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        page = page - 1;
                        Toast.makeText(MyShouCActivity.this, "暂无更多数据", Toast.LENGTH_LONG).show();
                        shouchang_xrecyler.loadMoreComplete();
                    }
                });
            }
        });
    }

    @OnClick(R.id.iv_goback)
    public void onViewClicked() {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
